#include "stdafx.h"
#if LIT_OGRE_SUPPORT

#include "LitCameraController.h"

namespace Lit
{
	LIT_API Ogre::Vector3 calculateAcceleration( Ogre::Real dt, Ogre::Real mass, const Ogre::Vector3& f )
	{
		return Ogre::Vector3( (f/mass)*dt );
	}
	CameraController::CameraController( Ogre::Camera * cam, Ogre::Real mass ) 
		: mPrivates(cam, mass)
	{ Ogre::Root::getSingleton().addFrameListener(this); }
	CameraController::~CameraController(){ Ogre::Root::getSingleton().removeFrameListener(this); }
				
	CameraControllerPrivates& CameraController::privates(){ return mPrivates; }

	bool CameraController::frameStarted( const Ogre::FrameEvent & evt )
	{
		if( !mPrivates->mCam || !mPrivates->mKeyboard ) return true;
		Ogre::Vector3 dir = mPrivates->mCam->getDirection();
		Ogre::Vector3 pos = mPrivates->mCam->getPosition();
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
		if( GetAsyncKeyState('Z') & 0x8000 )
		{
			mPrivates->mCam->setPosition(pos+calculateAcceleration( evt.timeSinceLastFrame, mPrivates->mMass, dir.normalisedCopy()*mPrivates->mMaxSpeed ));
		}
		else if( GetAsyncKeyState('S') & 0x8000 )
		{
			mPrivates->mCam->setPosition(pos-calculateAcceleration( evt.timeSinceLastFrame, mPrivates->mMass, dir.normalisedCopy()*mPrivates->mMaxSpeed ));
		}
#endif
		return true;
	}
};

#endif